package com.runda.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbjob.mapper.SysFunctionMapper;
import com.bbjob.mapper.SysRoleFunctionMapper;
import com.bbjob.mapper.SysUserRoleMapper;
import com.bbjob.mapper.UserMapper;
import com.bbjob.model.SysFunction;
import com.bbjob.model.SysRoleFunction;
import com.bbjob.model.SysUserRole;
import com.bbjob.model.User;
import com.bbjob.util.service.impl.BaseService;
import com.runda.system.service.MenuService;

import tk.mybatis.mapper.entity.Example;

@Service
public class MenuServiceImpl extends BaseService<SysFunction> implements MenuService {

	@Autowired
	private SysFunctionMapper menumapper;
	@Autowired
	private UserMapper usermapper;
	@Autowired
	private SysRoleFunctionMapper rolefunctionmapper;
	@Autowired
	private SysUserRoleMapper userrolemapper;
	
	@Override
	public int saveMenu(SysFunction menu) {
		// TODO Auto-generated method stub
		return menumapper.insertSelective(menu);
	}

	@Override
	public int editMenu(SysFunction menu) {
		// TODO Auto-generated method stub
		return menumapper.updateByPrimaryKeySelective(menu);
	}

	@Override
	public int deleteMenu(Integer id) {
		// TODO Auto-generated method stub
		return menumapper.deleteByPrimaryKey(id);
	}	
	
	public List<SysFunction> getMenuByPid(Integer pid) {
		Example example = new Example(SysFunction.class); 
		if(pid == null) {
			example.createCriteria().andIsNull("pId");
		}else{
			example.createCriteria().andEqualTo("pId", pid);
		}
		List<SysFunction> list = menumapper.selectByExample(example);
		if(list.size() == 0) {
			return new ArrayList<SysFunction>();
		}else {
			return list;
		}
	}
	
	@Override
	public List<SysFunction> selectTreeByPid(Integer pid) {
		List<SysFunction> resultList = new ArrayList<SysFunction>();
		List<SysFunction> list = getMenuByPid(pid);
		for (SysFunction menu : list) {
			SysFunction entity = new SysFunction();
			entity.setId(menu.getId());
			entity.setName(menu.getName());
			entity.setIconCls(menu.getIconCls());
			entity.setIsVisible(menu.getIsVisible());
			entity.setUrl(menu.getUrl());
			entity.setRemark(menu.getRemark());
			entity.setSortNo(menu.getSortNo());
			entity.getChildren().addAll(selectTreeByPid(menu.getId()));
			resultList.add(entity);
		}
		return resultList;
	}
	
	/**
	 * 只获取菜单，不获取特殊权限
	 * @param pid
	 * @return
	 */
	public List<SysFunction> getMenuOnly(Integer pid) {
		Example example = new Example(SysFunction.class); 
		Example.Criteria criteria = example.createCriteria();
		if(pid == null) {
			criteria.andIsNull("pId");
		}else{
			criteria.andEqualTo("pId", pid);
		}
		criteria.andNotEqualTo("type", 3);
		List<SysFunction> list = menumapper.selectByExample(example);
		if(list.size() == 0) {
			return new ArrayList<SysFunction>();
		}else {
			return list;
		}
	} 

	@Override
	public List<SysFunction> getMotherMenu(Integer userid) {
		User user = usermapper.selectByPrimaryKey(userid);
		if(user.getType() == -1) {
			List<SysFunction> function =  this.getMenuOnly(null);
			for (SysFunction sysFunction : function) {
				List<SysFunction> children = this.getMenuOnly(sysFunction.getId());
				sysFunction.setChildren(children);
			}
			return function;
		}else {
			//根据用户获取有几个role
			Example example = new Example(SysUserRole.class); 
			example.createCriteria().andEqualTo("userId", userid);
			//如果不是超管，需要验证role表，然后in查询菜单
			List<SysUserRole> list = userrolemapper.selectByExample(example);
			List<Integer> roleid = new ArrayList<>();
			for (SysUserRole sysUserRole : list) {
				roleid.add(sysUserRole.getRoleId());
			}
			Example ex = new Example(SysUserRole.class); 
			if(roleid.size() == 0) {
				return new ArrayList<>();
			}
			ex.createCriteria().andIn("roleId", roleid);
			List<SysRoleFunction> list2 = rolefunctionmapper.selectByExample(ex);
			List<Integer> menuid = new ArrayList<Integer>();
			for (SysRoleFunction sysRoleFunction : list2) {
				menuid.add(sysRoleFunction.getFunctionId());
			}
			if(menuid.size() == 0) {
				return new ArrayList<>();
			}
			Example ex2 = new Example(SysFunction.class); 
			ex2.createCriteria().andIn("id", menuid).andIsNull("pId").andNotEqualTo("type", 3);
			List<SysFunction> function =  menumapper.selectByExample(ex2);
			for (SysFunction sysFunction : function) {
				List<SysFunction> children = this.getChildrenMenu(userid, sysFunction.getId());
				sysFunction.setChildren(children);
			}
			return function;
		}
	}

	public List<SysFunction> getChildrenMenu(Integer userid,Integer pid) {
		User user = usermapper.selectByPrimaryKey(userid);
		if(user.getType() == -1) {
			return this.getMenuOnly(pid);
		}else {
			//根据用户获取有几个role
			Example example = new Example(SysUserRole.class); 
			example.createCriteria().andEqualTo("userId", userid);
			//如果不是超管，需要验证role表，然后in查询菜单
			List<SysUserRole> list = userrolemapper.selectByExample(example);
			List<Integer> roleid = new ArrayList<>();
			for (SysUserRole sysUserRole : list) {
				roleid.add(sysUserRole.getRoleId());
			}
			if(roleid.size() == 0) {
				return new ArrayList<>();
			}
			Example ex = new Example(SysUserRole.class); 
			ex.createCriteria().andIn("roleId", roleid);
			List<SysRoleFunction> list2 = rolefunctionmapper.selectByExample(ex);
			List<Integer> menuid = new ArrayList<Integer>();
			for (SysRoleFunction sysRoleFunction : list2) {
				menuid.add(sysRoleFunction.getFunctionId());
			}
			if(menuid.size() == 0) {
				return new ArrayList<>();
			}
			Example ex2 = new Example(SysFunction.class); 
			ex2.createCriteria().andIn("id", menuid).andEqualTo("pId",pid).andNotEqualTo("type", 3);;
			return menumapper.selectByExample(ex2);
		}
	}

	@Override
	public List<SysFunction> getOperationAuth(Integer userid) {
		User user = usermapper.selectByPrimaryKey(userid);
		if(user.getType() == -1) {
			Example example = new Example(SysFunction.class); 
			example.createCriteria().andEqualTo("type", 3);
			List<SysFunction> list = menumapper.selectByExample(example);
			return list;
		}else {
			//根据用户获取有几个role
			Example example = new Example(SysUserRole.class); 
			example.createCriteria().andEqualTo("userId", userid);
			//如果不是超管，需要验证role表，然后in查询菜单
			List<SysUserRole> list = userrolemapper.selectByExample(example);
			List<Integer> roleid = new ArrayList<>();
			for (SysUserRole sysUserRole : list) {
				roleid.add(sysUserRole.getRoleId());
			}
			Example ex = new Example(SysUserRole.class); 
			ex.createCriteria().andIn("roleId", roleid);
			List<SysRoleFunction> list2 = rolefunctionmapper.selectByExample(ex);
			List<Integer> menuid = new ArrayList<Integer>();
			for (SysRoleFunction sysRoleFunction : list2) {
				menuid.add(sysRoleFunction.getFunctionId());
			}
			Example ex2 = new Example(SysFunction.class); 
			ex2.createCriteria().andIn("id", menuid).andEqualTo("type", 3);
			List<SysFunction> function =  menumapper.selectByExample(ex2);
			return function;
		}
	}

}